package com.rightsidetech.system.service.impl;

import com.rightsidetech.system.entity.SysUserLoginHistory;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.mapper.SysUserLoginHistoryMapper;
import com.rightsidetech.system.mapper.SysUserLoginInfoMapper;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: yhr
 * @Date: 2021-10-15 14:38
 */
@Slf4j
@Service("sysUserLoginInfoServiceImpl")
public class SysUserLoginInfoServiceImpl implements SysUserLoginInfoService {
    @Resource
    private SysUserLoginInfoMapper sysUserLoginInfoMapper;
    @Resource
    private SysUserLoginHistoryMapper sysUserLoginHistoryMapper;

    @Override
    public String getLoginName(String tokenCompany) {
        SysUserLoginInfo loginInfo = sysUserLoginInfoMapper.selectByToken(tokenCompany);
        if (StringUtils.isEmpty(loginInfo)){
            log.info("用户不存在");
            return null;
        }

        SysUserLoginHistory history = sysUserLoginHistoryMapper.findOneByMaxId(loginInfo.getId());
        if (StringUtils.isEmpty(history)){
            log.info("该用户未登录");
            return null;
        }
        if (!StringUtils.isEmpty(history.getLogoutTime())){
            log.info("该账户已登出");
            return null;
        }else if (new Date().after (history.getEffectiveTime())){
            log.info("该账户有效时间过期");
            return null;
        }
        return loginInfo.getLoginName();
    }

    @Override
    public SysUserLoginInfo selectByLoginInfo(Long loginInfo) {
        return sysUserLoginInfoMapper.selectByPrimaryKey(loginInfo);
    }
}
